<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <style>
            .clear {
                height: 20vh;
                background-color: red;
                margin: 20px 0;
            }

            #aaa {
                /* opacity: 0; */
                /* height: 0; */
                /* position: fixed; */
                position: relative;
                top: 0;
            }
        </style>
    </head>
    <body>
        <h1 class="clear"></h1>
        <h1 class="clear"></h1>
        <h1 class="clear"></h1>
        <h2 id="aaa">Should anchor to here</h2>

        <table style="width: 400px" border="1">
            <tr>
                <td colspan="2">A</td>
            </tr>
            <tr>
                <td rowspan="2">B</td>
                <td></td>
            </tr>
            <tr>
                <td colspan="2">D</td>
            </tr>
            <tr>
                <td colspan="2">E</td>
            </tr>
        </table>
        <h1 class="clear"></h1>
        <h1 class="clear"></h1>
        <h1 class="clear"></h1>
        <a href="#aaa">click to go to anchor</a>
        <h1 class="clear"></h1>
        <h1 class="clear"></h1>
        <h1 class="clear"></h1>
    </body>

    <script>
        /**
         * 获取字符串中，某个字符的所有下标
         * @param {String} letter 搜索字符
         * @param {String} str 字符串
         */
        function findAllIndex(letter, str) {
            let ret = [];
            let arrStr = str.split('');
            let letterIndex = arrStr.indexOf(letter, 0);
            while (letterIndex >= 0) {
                ret.push(letterIndex);
                letterIndex = arrStr.indexOf(letter, letterIndex + 1);
            }

            return ret;
        }

        /**
         * 2进制补全
         * @param {string} str
         * @param {number} len
         */
        function fillZeroBinary(str, len = 8) {
            while (str.length < len) {
                str = '0' + str;
            }

            return str;
        }

        /**
         * 生成一个初始值为自身的数组
         * @param {number} len
         */
        function newArrayDefault(len) {
            return new Array(len).fill(-1).map((_, idx) => idx);
        }

        function new2DArray(row, col, defaultValue = -1) {
            const ret = [];
            for (let r = 0; r < row; r++) {
                let arr_row = [];
                for (let y = 0; y < col; y++) {
                    arr_row.push(defaultValue);
                }
                ret.push(arr_row);
            }
            return ret;
        }

        function newID() {
            // 获取当前时间戳
            let timestamp = Date.now().toString(36);

            // 生成一个随机数
            let randomNumber = Math.random().toString(36).slice(2, 5);

            // 可选：添加其他信息，例如当前页面的URL或用户信息等
            // let additionalInfo = window.location.href; // 仅作为示例，实际中可能需要哈希或其他处理

            // 组合时间戳和随机数（以及其他信息，如果有的话）
            // 如果添加了additionalInfo，确保对其进行适当的处理以减少长度和避免特殊字符
            let id = timestamp + randomNumber;

            // 可选：将ID转换为其他格式，例如Base64

            return id;
        }

        /**
         * odd奇数 even偶数
         * @param {number} x
         * @returns
         */
        function isOdd(x) {
            return x & 1 ? true : false;
        }

        function log(str) {
            console.log(str);
        }

        function IP2String(strIP) {
            let arr = strIP.split('.');

            let acc = '';
            acc = arr.reduce(
                (acc, cur) =>
                    (acc += fillZeroBinary(parseInt(cur).toString(2))),
                acc
            );
            log(`${strIP} => ${acc}`);
            return parseInt(acc, 2);
        }

        /** 校验整数，正整数和负整数都为true，其他为false */
        function isNumber(str) {
            str = '' + str;
            let regex = /^[\-\+]?[\d]+$/g;
            return regex.test(str);
        }

        function testFunction(arr, fun) {
            log('[testFunction]');
            arr.forEach((ele) => {
                console.log(`test ${JSON.stringify(ele)}`);
                console.log(fun(ele));
            });
        }
    </script>

    <script>
        let adder = {
            base: 1,
            add: function (arg) {
                let fun = (val) => val + this.base;
                return fun(arg);
            },
            addCall: function (arg) {
                let fun = function (val) {
                    return val + this.base;
                };
                let obj = {
                    base: 2,
                };
                return fun.call(obj, arg);
            },
        };
        console.log(adder.add(1));
        console.log(adder.addCall(1));

        let promise = new Promise(function (resolve, reject) {
            console.log('Promise');
            resolve();
        });

        promise.then(function () {
            console.log('resolved.');
        });

        console.log('Hi!');

        // Promise
        // Hi!
        // resolved

        console.log('-'.repeat(30));
        console.log('Number test');
        console.log(Number(null) === 0);
        console.log(Number(' ') === 0);
        console.log(Number(true) === 1);
    </script>
</html>
